﻿Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB
Imports Abyx.WindowsExtendedControls

Public Class SQC_Manufacturers_Form
    Implements ICarfDataEditingChildWindow

#Region "Declarations"
    Dim iDataObject As ManufacturerType
#End Region

#Region "Events"
    Private Sub Manufacturers_Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        InitializeControls()
        Me.InitializeThisFormAndControlSettings()
    End Sub
    Private Sub SQC_Manufacturers_Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub
    Private Sub Manufacturers_DetailListBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles Manufacturers_DetailListBox.SelectedIndexChanged
        If Me.Manufacturers_DetailListBox.SelectedIndex <> -1 Then
            Dim detail As DetailListBoxItem = Me.Manufacturers_DetailListBox.SelectedItem
            Me.iDataObject = detail.DataObject
            Refresh_ControlsWithDataObject()
            Manufacturers_DetailListBox.Top = Manufacturers_DetailListBox.SelectedIndex
        Else
            Me.iDataObject = Nothing
        End If
    End Sub
    Private Sub Logo_EdPicureBox_AddImage_Click(sender As System.Object, e As System.EventArgs) Handles Logo_EdPicureBox.AddImage_Click
        If PictureFile_OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Logo_EdPicureBox.Image = Image.FromFile(PictureFile_OpenFileDialog.FileName)
        End If
    End Sub
    Private Sub AddRecord_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles AddRecord_ImageButton.Click

        Refresh_DataObjectWithControls()
        Me.iDataObject.ManufacturerId = Guid.NewGuid
        Dim sqcMan As New SqcManager(AppSettings.LoggedUser)
        sqcMan.Update_Manufacturer(Me.iDataObject)
        Refresh_ManufacturesDbList()
        SVCM_Form.StatusInfoMsgPut("Manufacturer " & Me.iDataObject.Name & " has been added")
        Me.OrderRefreshRelatedDataControls(Me.iDataObject, FormActionOnDataEnum.NewRecord)

    End Sub
    Private Sub UpdateRecord_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles UpdateRecord_ImageButton.Click

        Refresh_DataObjectWithControls()
        Dim sqcMan As New SqcManager(AppSettings.LoggedUser)
        sqcMan.Update_Manufacturer(Me.iDataObject)
        Refresh_ManufacturesDbList()
        SVCM_Form.StatusInfoMsgPut("Manufacturer " & Me.iDataObject.Name & " has been updated")
        Me.OrderRefreshRelatedDataControls(Me.iDataObject, FormActionOnDataEnum.UpdateRecord)

    End Sub
    Private Sub DeleteRecord_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles DeleteRecord_ImageButton.Click

        Refresh_DataObjectWithControls()
        Dim sqcMan As New SqcManager(AppSettings.LoggedUser)
        sqcMan.Delete_Manufacturer(Me.iDataObject)
        Refresh_ManufacturesDbList()
        SVCM_Form.StatusInfoMsgPut("Manufacturer " & Me.iDataObject.Name & " has been deleted")
        Me.OrderRefreshRelatedDataControls(Me.iDataObject, FormActionOnDataEnum.DeleteRecord)

    End Sub
#End Region

#Region "Private Procedures"
    Private Sub InitializeControls()
        Refresh_ManufacturesDbList()
    End Sub
    Private Sub Refresh_ManufacturesDbList()
        Dim sqcMan As New SqcManager(AppSettings.LoggedUser)
        Dim mans As DB.ManufacturerTypeCollection = sqcMan.Get_Manufacturers_All
        Manufacturers_DetailListBox.Items.Clear()
        Dim selIndex As Integer = -1
        For Each man As DB.ManufacturerType In mans
            If Me.iDataObject IsNot Nothing Then
                If Me.iDataObject = man Then
                    selIndex = Manufacturers_DetailListBox.Items.Add(New DetailListBoxItem(man.Name, man.Website, man.Contacts, man.Logo, man))
                Else
                    Manufacturers_DetailListBox.Items.Add(New DetailListBoxItem(man.Name, man.Website, man.Contacts, man.Logo, man))
                End If
            Else
                Manufacturers_DetailListBox.Items.Add(New DetailListBoxItem(man.Name, man.Website, man.Contacts, man.Logo, man))
            End If
        Next

        If selIndex = -1 Then
            If Manufacturers_DetailListBox.Items.Count > 0 Then
                Manufacturers_DetailListBox.SelectedIndex = 0
                Manufacturers_DetailListBox.Top = 0
            End If
        Else
            Manufacturers_DetailListBox.SelectedIndex = selIndex
            Manufacturers_DetailListBox.Top = Manufacturers_DetailListBox.SelectedIndex = selIndex
        End If

    End Sub
#End Region

#Region "ICARF Implementations"
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, _
                                               Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls
        Dim main As ICarfParentWindow = Me.MdiParent
        main.OrderRefreshRelatedDataControls(Me, _
                                             RelatedControlDataCategoriesEnum.Manufacturers, _
                                             DataItem, _
                                             ActionOnDataItem)
    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, _
                                                 DataTypeCategory As RelatedControlDataCategoriesEnum, _
                                                 Optional DataItem As Object = Nothing, _
                                                 Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls

        'If DataTypeCategory = RelatedControlDataCategoriesEnum.Manufacturers Then
        '    If ActionOnDataItem = FormActionOnDataEnum.DeleteRecord Then
        '        If (DataItem IsNot Nothing) _
        '            AndAlso (TypeOf DataItem Is ManufacturerType) Then
        '            Dim obj As ManufacturerType = DataItem
        '            If obj = Me.iDataObject Then
        '                AMsg.ShowDialog("The data being edited has been updated", MsgBoxStyle.Exclamation)
        '                Me.Close()
        '            End If
        '        End If
        '    End If
        'End If

    End Sub
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject
        Return Nothing
    End Function
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject
        If Me.iDataObject IsNot Nothing Then
            With Me.iDataObject
                Me.Id_TextBox.Text = Me.iDataObject.ManufacturerId.ToString
                Me.Name_TextBox.Text = Me.iDataObject.Name
                Me.Description_TextBox.Text = Me.iDataObject.Description
                Me.WebSite_TextBox.Text = Me.iDataObject.Website
                Me.Email_TextBox.Text = Me.iDataObject.Email
                Me.Contacts_TextBox.Text = Me.iDataObject.Contacts
                Me.Logo_EdPicureBox.Image = Me.iDataObject.Logo
            End With
        End If
    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls
        Dim dObject As New ManufacturerType
        With dObject
            If Id_TextBox.Text = "" Then
                Id_TextBox.Text = Guid.NewGuid.ToString
            End If
            Me.iDataObject.ManufacturerId = New Guid(Id_TextBox.Text)
            Me.iDataObject.Name = Name_TextBox.Text
            Me.iDataObject.Description = Description_TextBox.Text
            Me.iDataObject.Website = WebSite_TextBox.Text
            Me.iDataObject.Email = Email_TextBox.Text
            Me.iDataObject.Contacts = Contacts_TextBox.Text
            Me.iDataObject.Logo = Me.Logo_EdPicureBox.Image
        End With
    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
#End Region

End Class